package com.yang.dao;

import com.yang.entity.Ads;

import java.sql.*;
import java.util.*;

public class AdsDao {
    //查询五个轮播图
    public List<Ads> queryAds() {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("select * from Ads where target is not null and target <> ''  order by target asc limit 5");
            rs = ps.executeQuery();
            List<Ads> list = new ArrayList<>();
            while (rs.next()) {
                Ads obj = new Ads();
                obj.setId(rs.getInt("id"));
                obj.setTitle(rs.getString("title"));
                obj.setRemark(rs.getString("remark"));
                obj.setImg(rs.getString("img"));
                obj.setUrl(rs.getString("url"));
                obj.setTarget(rs.getString("target"));
                list.add(obj);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(rs, ps, conn);
        }
        return null;
    }
    //查询全部挂广告
    public List<Ads> QuseryAdss() {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("select * from Ads ");
            rs = ps.executeQuery();
            List<Ads> list = new ArrayList<>();
            while (rs.next()) {
                Ads obj = new Ads();
                obj.setId(rs.getInt("id"));
                obj.setTitle(rs.getString("title"));
                obj.setRemark(rs.getString("remark"));
                obj.setImg(rs.getString("img"));
                obj.setUrl(rs.getString("url"));
                obj.setTarget(rs.getString("target"));
                list.add(obj);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(rs, ps, conn);
        }
        return null;
    }
    //单个查询
    public Ads queryAdsById(Integer id) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("select * from Ads where id=?");
            ps.setInt(1, id);
            rs = ps.executeQuery();
            if (rs.next()) {
                Ads obj = new Ads();
                obj.setId(rs.getInt("id"));
                obj.setTitle(rs.getString("title"));
                obj.setRemark(rs.getString("remark"));
                obj.setImg(rs.getString("img"));
                obj.setUrl(rs.getString("url"));
                obj.setTarget(rs.getString("target"));
                return obj;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(rs, ps, conn);
        }
        return null;
    }

    //添加
    public int saveAds(Ads obj) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("insert into ads(title, remark, img, url, target) values (?, ?, ?, ?, ?)");
            ps.setString(1, obj.getTitle());
            ps.setString(2, obj.getRemark());
            ps.setString(3, obj.getImg());
            ps.setString(4, obj.getUrl());
            ps.setString(5, obj.getTarget());
            return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(null, ps, conn);
        }
        return 0;
    }
    //sql修改
    public int updateBySql(String sql) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        try {
            ps=conn.prepareStatement(sql);
            return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(null, ps, conn);
        }
        return 0;
    }
    //删除
    public int deleteAdsById(Integer id) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("delete from Ads where id=?");
            ps.setInt(1, id);
            return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(null, ps, conn);
        }
        return 0;
    }

    //修改
    public int updateAdsById(Ads obj) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("update ads set title=?, remark=?, img=?, url=?, target=? where id=?");
            ps.setString(1, obj.getTitle());
            ps.setString(2, obj.getRemark());
            ps.setString(3, obj.getImg());
            ps.setString(4, obj.getUrl());
            ps.setString(5, obj.getTarget());
            ps.setInt(6, obj.getId());
            return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(null, ps, conn);
        }
        return 0;
    }
}
